var config = require("config");
cc.Class({
    extends: cc.Component,

    properties:()=> ({
        blowupani: {
            default: null,
            type:cc.Prefab,
            tooltip: '爆炸动画',
        },
        fuhuo_ani: {
            default: null,
            type:cc.Prefab,
            tooltip: '复活动画',
        },
        speed_clearscreen: cc.Node,
        manget_xi_ani: cc.Node,
        hero_speedtop: cc.Node,
        main: {
            default: null,
            type: require('main'),
        },
        bulletGroup: {
            default: null,
            type: require('bulletGroup'),
        },
        img_hero: cc.Node,
        img_pet: cc.Node,
        sprArrPet :{
            default:[],
            type:[cc.SpriteFrame],
        },
        touchNode: cc.Node,
    }),
    // use this for initialization
    onLoad: function () {
        this.eState = D.commonInfo.gameState.none;
        cc.director.getCollisionManager().enabled=true;
        this.speed_clearscreen.active = false;

        this.startPos = cc.v2(0,0);
    
        if(config.CurHeroIndex == 4 || config.CurHeroIndex == 5){
            this.manget_xi_ani.active = true;
            config.isToHero = true;
        }else{
            this.manget_xi_ani.active = false;
        }
        this.fuhuoyici = true;
        this.collider = this.getComponent(cc.PolygonCollider);
        
        this.showHeroByIndex(config.CurHeroIndex);
        cc.vv.gameDataMgr.saveCurHeroIndex(config.CurHeroIndex);

        this.img_pet.active = false;
        if(config.CurPetIndex == -1){
        }else{
            this.img_pet.active = true;
            this.showPet(config.CurPetIndex);
        }
        cc.vv.gameDataMgr.saveCurPetIndex(config.CurPetIndex);
        console.log("getHeroPos:",this.getHeroPos());
        this.onDrag();
    },
    showHeroByIndex(index){
        console.log(">>>>>>index:",index);
        config.loadImage(this.img_hero,"avatar_"+index,"resources/image/");
    },
    showPet(index){
        console.log(">>>>>>petindex:",index);
        this.img_pet.getComponent(cc.Sprite).spriteFrame = this.sprArrPet[index];
    },
    setMangetXiAni(blt){//设置吸星动画
        this.manget_xi_ani.active = blt;
    },
    setHeroState: function (state) {
        if(state == 1){
            this.speed_clearscreen.active = true;
            this.collider.points = [cc.v2(-250,-100),cc.v2(250,-100),cc.v2(250,250),cc.v2(-250,250)];
        }else if(state == 0){
            this.speed_clearscreen.active = false;
            this.collider.points = [cc.v2(-45,-80),cc.v2(45,-80),cc.v2(45,80),cc.v2(-45,80)];
        }
    },
    getHeroPos(){
        return this.node.getPosition();
    },
    //添加拖动监听
    onDrag: function(){
        this.touchNode.on(cc.Node.EventType.TOUCH_START, this.dragStart, this);
        this.touchNode.on('touchmove', this.dragMove, this);
    },
    //去掉拖动监听
    offDrag: function(){ 
         this.touchNode.off(cc.Node.EventType.TOUCH_START, this.dragStart, this);
         this.touchNode.off('touchmove', this.dragMove, this);
    },
    dragStart: function(event){
        var locationv = event.getLocation();
        var location = this.node.parent.convertToNodeSpaceAR(locationv);
        // console.log("location>>>:",location);
        this.startPos=location;
    },
    //拖动
    dragMove: function(event){ 
        var locationv = event.getLocation();
        var location = this.node.parent.convertToNodeSpaceAR(locationv);
        var despos = cc.v2(location.x-this.startPos.x,location.y-this.startPos.y);
        
        var selfPos = this.getHeroPos();
        selfPos.x +=  despos.x;
        selfPos.y +=  despos.y;
        //自己不移出屏幕 
        var minX = -this.node.parent.width/2+this.node.width/2;
        var maxX = -minX;
        var minY = -this.node.parent.height/2+this.node.height/2;
        var maxY = -minY;
        if (selfPos.x< minX){
            selfPos.x = minX;
        }
        if (selfPos.x>maxX){
            selfPos.x = maxX;
        }
        if (selfPos.y< minY){
            selfPos.y = minY;
        }
        if (selfPos.y> maxY){
            selfPos.y = maxY;
        }
        this.node.setPosition(selfPos);
        this.startPos=location;
    },
    dragMove2: function(event){
        var locationv = event.getLocation();
        var location = this.node.parent.convertToNodeSpaceAR(locationv);
        //自己不移出屏幕 
        var minX = -this.node.parent.width/2+this.node.width/2;
        var maxX = -minX;
        var minY = -this.node.parent.height/2+this.node.height/2;
        var maxY = -minY;
        if (location.x< minX){
            location.x = minX;
        }
        if (location.x>maxX){
            location.x = maxX;
        }
        if (location.y< minY){
            location.y = minY;
        }
        if (location.y> maxY){
            location.y = maxY;
        }
        this.node.x = location.x;
    },
    //碰撞监测
    onCollisionEnter: function(other,self) {
        // console.log("hero 碰撞监测 other.node.group:",other.node.group)
        if (other.node.group == 'ufo'){
            if(other.node.name == 'ufoCyclone'){
                if(config.HeroState == 1){
                }else{
                    //播放动画
                    var po = this.node.getPosition();
                    var blowup = cc.instantiate(this.blowupani);
                    this.node.parent.addChild(blowup);
                    blowup.setPosition(po);
                    var animation = blowup.getComponent(cc.Animation);
                    animation.on('finished',  this.onFinished, blowup);
                    this.main.heroHpNum-=config.HeroMaxHp; 
                    this.main.setHeroHp(this.main.heroHpNum);
                }
            }
        }else if(other.node.group == 'enemy'){
            var enemy = other.node.getComponent('enemy');
            // console.log("enemy initHP:",enemy.initHP)
            if(config.HeroState == 1 || config.CurHeroWuDi){
                // console.log("1111111111:",other)
            }else{
                //播放动画
                var po = this.node.getPosition();
                var blowup = cc.instantiate(this.blowupani);
                this.node.parent.addChild(blowup);
                blowup.setPosition(po);
                var animation = blowup.getComponent(cc.Animation);
                animation.on('finished',  this.onFinished, blowup);
                this.main.heroHpNum-=enemy.initHit;
                this.main.setHeroHp(this.main.heroHpNum);
            }
            // //播放音效
            // cc.audioEngine.playEffect(this.gameOverClip, false);
            // //清除节点
            // this.node.destroy();
            // //更新分数 
            // this.main.gameOver();   
        }else if(other.node.group == 'bossBullet'){
            // var bossBullet = other.node.getComponent('bossBullet');
            // console.log("bossBullet :",bossBullet)
            if(config.HeroState == 1 || config.CurHeroWuDi){
                return;
            }

            //播放动画
            var po = this.node.getPosition();
            var blowup = cc.instantiate(this.blowupani);
            this.node.parent.addChild(blowup);
            blowup.setPosition(po);
            var animation = blowup.getComponent(cc.Animation);
            animation.on('finished',  this.onFinished, blowup);
            this.main.heroHpNum-=config.BossBulletHpDrop;
            this.main.setHeroHp(this.main.heroHpNum);
        }else{
            return false;
        }

        //判断自己死亡
        if(this.main.heroHpNum <=0){
            cc.vv.audioMgr.playSFX("ch_die");
            this.hideHero();
            if((config.CurHeroIndex == 2 || config.CurHeroIndex == 5)&&this.fuhuoyici){
                //免费复活一次 fuhuo_ani
                this.fuhuoyici = false;
                this.main.gameOver(false);
                this.fuhuoAni();
            }else{
                this.main.gameOver(true);
            }
        }
        
    },
    //复活动画
    fuhuoAni(){
        var po = this.node.getPosition();
        var fuhuo = cc.instantiate(this.fuhuo_ani);
        this.node.parent.addChild(fuhuo);
        fuhuo.setPosition(po);
        var animation = fuhuo.getComponent(cc.Animation);
        animation.on('finished',  this.onFuhuoFinished, fuhuo);
        this.fuhuoFuc();
    },
    //隐藏自己
    hideHero(){
        this.img_hero.active = false;
        this.img_pet.active = false;
        this.hero_speedtop.active = false;
    },
    //显示自己
    showHero(){
        this.img_hero.active = true;
        if(config.CurPetIndex != -1){
            this.img_pet.active = true;
        }
        this.hero_speedtop.active = true;
    },
    onFinished: function(event) { //爆炸动画结束后
        this.destroy();
    },
    onFuhuoFinished: function(event) { //复活动画结束后
        this.destroy();
    },
    fuhuoFuc(){
        var self = this;
        var fn = function(){
            self.showHero();
            self.main.fuhuo();

            //3秒无敌状态
            config.CurHeroWuDi = true;
            var fn2 = function(){
                config.CurHeroWuDi = false;
            }
            setTimeout(fn2,3000);
        }
        setTimeout(fn,1200);
    }
});
